#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;

int n, m;
struct Node {
  int p, d;
} a[N];
bool cmp(const Node& a, const Node& b) { return a.d < b.d; }
void solve() {
  rep(i, 1, n) cin >> a[i].p >> a[i].d;
  sort(a + 1, a + n + 1, cmp);
  priority_queue<int, vector<int>, greater<int>> pq;
  int t = 0;
  ll sum = 0;
  rep(i, 1, n) {
    if (t < a[i].d) {
      sum += a[i].p;
      pq.push(a[i].p);
      t++;
    } else if (a[i].p > pq.top()) {
      sum += a[i].p - pq.top();
      pq.pop();
      pq.push(a[i].p);
    }
  }
  cout << sum << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (cin >> n) solve();
  return 0;
}